
package artistic;

/**
 * Canvas blender for ordinary layer blending
 * 
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */

public class NormalBlender implements CanvasBlender{
    public void apply(CanvasTile dest, CanvasTile source)
    {
        int[] dArray = dest.getRgbaArray();
        int[] sArray = source.getRgbaArray();
        for(int i=0, length = dArray.length;
            i<length; ++i)
        {
            int sColor = sArray[i];
            if((sColor&0xff000000) == 0xff000000)
            {
                dArray[i] = sColor;
                continue;
            }else if((sColor&0xff000000) == 0)
                continue;
            
            int dColor = dArray[i];
            int[] sArr = CanvasTile.fromPackedRGBA(sColor);
            int[] dArr = CanvasTile.fromPackedRGBA(dColor);
            dArr[0] = ((dArr[0]*(256-sArr[3]))+(sArr[0]*sArr[3]))>>8;
            dArr[1] = ((dArr[1]*(256-sArr[3]))+(sArr[1]*sArr[3]))>>8;
            dArr[2] = ((dArr[2]*(256-sArr[3]))+(sArr[2]*sArr[3]))>>8;
            dArr[3] = Math.min(255, dArr[3]+sArr[3]);
            dArray[i] = CanvasTile.toPackedRGBA(dArr);
        }
        dest.setChanged(true);
    }
}
